package com.rural.bus.adapter;

import java.util.List;

import org.json.JSONObject;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.text.Html;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.rural.bus.MyApplication;
import com.rural.bus.R;
import com.rural.bus.adapter.base.MyBaseAdapter;
import com.rural.bus.adapter.base.ViewHolder;
import com.rural.bus.model.Order;
import com.rural.bus.model.Railway;
import com.rural.bus.net.OnNetCallback;
import com.rural.bus.utils.DateUtil;
import com.rural.bus.utils.DialogUtil;
import com.rural.bus.utils.ProgressUtil;

public class OrderAdapter extends MyBaseAdapter<Order> {
	private List<Order > mOrders;

	public OrderAdapter(Context context, List<Order> orders) {
		super(context, orders);
		this.mOrders=orders;
		this.mContext=context;
	}
	
	@Override
	public View getView(int arg0, View convertView, ViewGroup arg2) {
		if(convertView==null){
			convertView=mInflater.inflate(R.layout.item_my_order_list, null);
		}
		TextView tvStartLoc=ViewHolder.get(convertView, R.id.tv_start_loc);
		TextView tvEndLoc=ViewHolder.get(convertView, R.id.tv_end_loc);
		TextView tvUsername=ViewHolder.get(convertView, R.id.tv_username);
		TextView tvTime=ViewHolder.get(convertView, R.id.tv_take_time);
//		TextView tvOrderPaymethod=ViewHolder.get(convertView, R.id.orderpaymethod);
//		tvOrderTime=ViewHolder.get(convertView, R.id.ordertime);
//		TextView tvResponer=ViewHolder.get(convertView, R.id.order_responer);
//		btDelive=ViewHolder.get(convertView, R.id.delive);
		ImageView ivCall=ViewHolder.get(convertView, R.id.iv_call);
		TextView tvStatus=ViewHolder.get(convertView, R.id.tv_status);
		TextView tvCancel=ViewHolder.get(convertView, R.id.tv_cancel);
		
		tvCancel.setText(Html.fromHtml("<u>取消订单</u>"));
//      	tvStatus.setVisibility(View.GONE);
//		tvStatus.setText("结束");
		
		final Order order=mOrders.get(arg0);
        tvEndLoc.setText(order.endLocation);
        tvStartLoc.setText(order.startLocation);
        tvUsername.setText("乘车人："+order.userName);
        tvTime.setText(DateUtil.getStr(order.takeTime,"yyyy年MM月dd日 HH:mm"));
        tvStatus.setText(order.getOrderStatus());
		if(order.orderStatus>Order.ORDERSTATUS_READYED){
			tvCancel.setVisibility(View.GONE);
		}else{
			tvCancel.setVisibility(View.VISIBLE);
		}

      	ivCall.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				Intent intent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:"+order.driverPhone));  
                mContext.startActivity(intent); 
			}
		});
      	
      	tvCancel.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				DialogUtil.dialog(mContext, "确认取消订单吗？", "是", new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int arg1) {
						cancel(order);
						
						dialog.dismiss();
					}
				}, "否", new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
					}
				});
			}
		});
      	
		return convertView;
	}
	
	/***
	 * 取消订单
	 * @param order
	 */
	private void cancel(final Order order){
		ProgressUtil.showProgress(mContext, "");
		MyApplication app=(MyApplication) mContext.getApplicationContext();
		Railway.cancel(app.getCurrentUser().userId, order.orderId, "", new OnNetCallback() {
			
			@Override
			public void success(JSONObject object) {
				ProgressUtil.closeProgress();
				toast("取消成功");
				list.remove(order);
				notifyDataSetChanged();
			}
			
			@Override
			public void fail(String error) {
				ProgressUtil.closeProgress();
				toast("取消失败 ");
				
			}
		});
	}

}
